package com.customer.crm.business.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.customer.crm.business.pojo.Business;
import com.customer.crm.business.pojo.BusinessCustomer;

import com.customer.crm.business.service.Impl.BusinessCustomerServiceImpl;
import com.customer.crm.business.service.Impl.BusinessServiceImpl;
import com.customer.crm.customer.pojo.Contacts;
import com.customer.crm.customer.service.impl.ContactsServiceImpl;
import com.customer.crm.customer.pojo.Customer;
import com.customer.crm.customer.service.impl.CustomerServiceImpl;
import com.customer.crm.system.pojo.SystemUser;
import com.customer.crm.system.service.impl.SystemUserServiceImpl;
import com.customer.crm.util.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * The type Business action list.
 */
@Controller
public class BusinessActionList {
    @Autowired
    private BusinessServiceImpl businessService;
    @Autowired
    private CustomerServiceImpl customerService;
    @Autowired
    private BusinessCustomerServiceImpl businessCustomerService;
    @Autowired
    private SystemUserServiceImpl systemUserService;
    @Autowired
    private ContactsServiceImpl contactsService;

    /**
     * To business string.
     *  跳转到list_business商机页面
     * @return the string
     */
    @GetMapping("/system/business/BusinessAction_list.action")
    public String toBusiness() {
        return "views/manage/business/list_business";
    }

    /**
     * Find all business action model map.
     *  对所有的商机进行分页
     * @param page the page
     * @param rows the rows
     * @return the model map
     */
    @RequestMapping("/manage/business/BusinessAction_findByPage.action")
    @ResponseBody
    public ModelMap findAllBusinessAction(String page, String rows) {
        Integer p = Integer.parseInt(page);
        Integer r = Integer.parseInt(rows);
        ModelMap modelMap = new ModelMap();
        //分页
        List<BusinessCustomer> result = businessCustomerService.findAllBusinessCustomerPage((p-1)*r, r);
        int totalRows = result.size();
        modelMap.put("totalRows", totalRows);
        modelMap.put("result", result);
        ModelMap modelMap1 = new ModelMap();
        modelMap1.put("data", modelMap);
        return modelMap1;
    }

    /**
     * To see business string.
     *  获取关于商机的所有的字段，通过id去获取
     * @param id    the id
     * @param model the model
     * @return the string
     */
    @GetMapping("/manage/business/BusinessAction_see.action")
    public String toSeeBusiness(@RequestParam("businessId") String id, Model model) {
        //通过id获取所有商机
        Business byId = businessService.getOne(new QueryWrapper<Business>().eq("id", id));
        //通过商机绑定的用户外键id获取对应用户名
        SystemUser user = systemUserService.getOne(new QueryWrapper<SystemUser>().eq("id", byId.getOwnerUserId()));
        //通过商机绑定的客户id获取对应的客户
        Customer customer = customerService.getOne(new QueryWrapper<Customer>().eq("id", byId.getCustomerId()));
        Contacts contacts = contactsService.getOne(new QueryWrapper<Contacts>().eq("id", byId.getContactsId()));
        //放到model中
        model.addAttribute("busiVO", byId);
        model.addAttribute("busiVOUser", user);
        model.addAttribute("busiVOCustomer", customer);
        model.addAttribute("contacts",contacts);
        return "views/manage/business/see_business";
    }
    @GetMapping("/manage/business/BusinessAction_edit.action")
    public String toUpdateBusiness(@RequestParam("businessIdUpdate") String id,Model model){
        //通过id获取所有商机
        Business byId = businessService.getOne(new QueryWrapper<Business>().eq("id", id));

        //对类型进行筛选
        List<Business> bus = businessService.list(new QueryWrapper<Business>().select("distinct type"));
        //对商机来源进行去重
        List<Business> busOri = businessService.list(new QueryWrapper<Business>().select("distinct origin"));

        List<SystemUser> list = systemUserService.list(null);
        List<Customer> cus = customerService.list(null);
        List<Contacts> con = contactsService.list(null);
        //放到model中
        model.addAttribute("busiVO", byId);
        model.addAttribute("bus",bus);
        model.addAttribute("buss",busOri);
        model.addAttribute("list",list);
        model.addAttribute("cus",cus);
        model.addAttribute("con",con);
        return "views/manage/business/edit_business";
    }
    @PostMapping("/manage/business/BusinessAction_saveOrUpdate.action")
    @ResponseBody
    public ResultData updateBusiness(Business business){
        ResultData resultData = null;
        boolean b = businessService.updateById(business);
        if(b == true){
             resultData = ResultData.buildSuccessResult("修改成功");
        }else {
            resultData = ResultData.buildSuccessResult("修改失败");
        }
        return resultData;
    }

    @GetMapping("manage/business/BusinessAction_edt.action")
    public String toSaveBusiness(Model model){
        List<SystemUser> list = systemUserService.list(null);
        List<Customer> cus = customerService.list(null);
        List<Contacts> con = contactsService.list(null);
        //对类型进行筛选
        List<Business> bus = businessService.list(new QueryWrapper<Business>().select("distinct type"));
        //对商机来源进行去重
        List<Business> busOri = businessService.list(new QueryWrapper<Business>().select("distinct origin"));
        //放到model中
        model.addAttribute("bus",bus);
        model.addAttribute("buss",busOri);
        model.addAttribute("list",list);
        model.addAttribute("cus",cus);
        model.addAttribute("con",con);
        return "views/manage/business/index_business";
    }
    @PostMapping("/manage/business/BusinessAction_SaveBusiness.action")
    @ResponseBody
    public ModelMap saveBusiness(Business business){
        ModelMap modelMap = new ModelMap();
        int id = (int) (Math.random() * 1000);
        Business byId = businessService.getById(id);
        if (byId == null){
            business.setId(id);
        }else {
            int a = (int) (Math.random()*10);
            id+=a;
            business.setId(id);
        }
        boolean save = businessService.save(business);
        if (save == true){
            modelMap.addAttribute("success","添加成功");
        }else {
            modelMap.addAttribute("success","添加失败");
        }
        return modelMap;
    }
}
